﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.DataAccess.DataInf;
using FB.Utilities;

namespace FB.DataAccess
{
    public class VideoCategoryDAO : IVideoCategoryDAO
    {
        private static FBDATA dataContext;

        private IRepository<VideoCategory> repository;
        public VideoCategoryDAO()
        {
            dataContext = new FBDATA();
            repository = new RepositoryDAO<VideoCategory>(dataContext);
        }

        public IEnumerable<VideoCategory> GetAllVideoCategory()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.ID);
        }
        public VideoCategory GetVideoCategoryByID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();
        }
        public VideoCategory GetLastRecord()
        {
            return dataContext.VideoCategories.OrderByDescending(obj => obj.ID).FirstOrDefault();
        }

        public bool UpdateVideoCategory(VideoCategory inVideoCategory)
        {
            if (inVideoCategory == null) return false;
            VideoCategory upVideoCategory = repository.First(sobj => sobj.ID == inVideoCategory.ID);
            if (upVideoCategory == null) return false;
            upVideoCategory.Name = inVideoCategory.Name;
            upVideoCategory.Description = inVideoCategory.Description;
            upVideoCategory.Enable = inVideoCategory.Enable;
            return repository.SaveChanges() > 0;
        }
        public bool InsertVideoCategory(VideoCategory inVideoCategory)
        {
            if (inVideoCategory == null) return false;
            repository.Add(inVideoCategory);
            return repository.SaveChanges() > 0;
        }

        public ReturnValue<bool> DelVideoCategory(Guid id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                VideoCategory objDel = repository.First(obj => obj.ID == id);
                ret = DelVideoCategory(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelVideoCategory(VideoCategory inVideoCategory)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inVideoCategory == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inVideoCategory);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        public List<VideoCategory> GetVideoCateByVideoYoutubeID(string videoYoutubeID)
        {
            List<View_VideoInCategory> MessResult = (from m in dataContext.View_VideoInCategory
                                              where m.YoutubeID == videoYoutubeID
                                              select m).ToList();
            List<VideoCategory> Result = new List<VideoCategory>();
            foreach (View_VideoInCategory item in MessResult)
            {
                Result.Add(new VideoCategory(item));
            }
            return Result;
        }

        public List<VideoCategory> GetVideoCategoryLimit(int limit)
        {
            return (from m in dataContext.VideoCategories
                     orderby Guid.NewGuid()
                     select m).Take(limit).ToList();
        }
    }
}
